#include "qrsagsmapswipe.h"
#include "qgsmapsettings.h"
#include "qgsmaprendererparalleljob.h"

QRSAGSMapSwipe::QRSAGSMapSwipe(QgsMapCanvas *mapCanvas)
    : QgsMapCanvasItem(mapCanvas)
{
    this->mMapCanvas = mapCanvas;
    this->length = 0;
    this->isVertical = true;
    this->isPaint = false;
    this->setZValue(-9.0);
    this->flg = false;
}

QRSAGSMapSwipe::~QRSAGSMapSwipe()
{

}

void QRSAGSMapSwipe::clear()
{
    this->mMapLayers.clear();
    this->length = -1;
}

void QRSAGSMapSwipe::setLayers(QList<QgsMapLayer*> mapLayerList)
{
    this->mMapLayers.clear();
    this->mMapLayers = mapLayerList;
}

void QRSAGSMapSwipe::setIsVertical(bool isVertical)
{
    this->isVertical = isVertical;
}

void QRSAGSMapSwipe::setLength(int x, int y)
{
    y = this->boundingRect().height() - y;
    this->length = this->isVertical? x:y;
    this->update();
}

void QRSAGSMapSwipe::setImage(QImage image)
{
    this->copyImage = image;
}

QList<QgsMapLayer *> QRSAGSMapSwipe::getMapLayers()
{
    return this->mMapLayers;
}

void QRSAGSMapSwipe::paint(QPainter *painter)
{
    if(this->mMapLayers.size() == 0 || this->length == -1){
        return;
    }

    int width, height;
    QLine line;
    if(this->isVertical){
        width = this->length;
        height = this->boundingRect().height() - 2;
        line = QLine(width-1, 0, width-1, height -1);
    }else {
        width = this->boundingRect().width();
        height = this->boundingRect().height() - this->length;
        line = QLine(0, height-1, width-1, height -1);
    }

    QImage image = this->copyImage.copy(0,0, width, height);
    painter->drawImage(QRectF(0,0, width, height), image);
    painter->drawLine(line);
}
